/*公用组件*/
import Vue from "vue";
import Vuex from "vuex";
import App from "./App";
import router from "./router"
/*Element-UI组件*/
import Element from "element-ui"
import "element-ui/lib/theme-chalk/index.css";
/*富文本编辑器组件*/
import VueUeditorWrap from 'vue-ueditor-wrap';
import '../static/UEditor/third-party/jquery-1.10.2.js';
import '../static/UEditor/ueditor.config.js';
import '../static/UEditor/ueditor.all.js';
import '../static/UEditor/ueditor.parse.min.js';
import '../static/UEditor/lang/zh-cn/zh-cn.js';
/*全局变量*/
import store from './store/index';
/*缓存变量*/
import Cookies from 'js-cookie';
/*Echarts*/
import * as echarts from 'echarts';
/*XLSX*/
import XLSX from "xlsx";

Vue.use(Element);
Vue.use(Vuex);
Vue.use(echarts);
Vue.use(XLSX);

Vue.config.productionTip = false;

Vue.prototype.$store = store;
Vue.prototype.$jump = router;
Vue.prototype.$echarts = echarts;

// 将VueUeditorWrap注册为全局组件
Vue.component('vue-ueditor-wrap', VueUeditorWrap);
window.UEDITOR_CONFIG.UEDITOR_HOME_URL = process.env.UEDITOR_HOME_URL;
window.UEDITOR_CONFIG.serverUrl = process.env.serverUrl;
import jquery from '../static/UEditor/third-party/jquery-1.10.2.js';

const timer = setInterval(function () {
  if (jquery('#ueditor_0').length > 0) {
    jquery('#ueditor_0').attr('src', '../static/UEditor/uedit.html');
    clearTimeout(timer)
  }
}, 10);


// 登录验证拦截
router.beforeEach((to, from, next) => {
  let isLogin = Cookies.get('SET_IS_LOGIN');
  let userId = Cookies.get('SET_USER_ID');
  let token = Cookies.get('SET_TOKEN');
  let name = Cookies.get('SET_NAME');
  let description = Cookies.get('SET_DESCRIPTION');
  let role = Cookies.get('SET_USER_ROLE');
  let permission = Cookies.get('SET_USER_PERMISSION');

  if (to.meta.requireAuth) { // 判断是否有权限
    if (!store.state.isLogin && !isLogin){
      next({path: "/login"});
    } else {
      store.commit('SET_USER_ID', userId);
      store.commit('SET_TOKEN', token);
      store.commit('SET_NAME', name);
      store.commit('SET_DESCRIPTION', description);
      store.commit('SET_USER_ROLE', role);
      store.commit('SET_USER_PERMISSION', permission);
      store.commit('SET_IS_LOGIN', true);
      if (to.path === "/login"){
        // 登录状态下访问登录页，跳转到首页
        next({path: "/home"});
      } else if (to.path === "/") {
        if (role === '95' && to.path !== '/QAExpertManage'){
          next({path: '/QAExpertManage'});
        } else {
          next({path: "/home"});
        }
      } else {
        next();
      }
    }
  } else {
    next();
  }
});

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
});
